<?php
/**
 * 数据库连接类[沿用老PATEO框架的mysql连接]
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0 DBConnect add by yaoli<yaoli@pateo.com.cn> 2012/12/05 11:00
 */
class_exists('\pateo\general\systemdriver\db_factory') || include( 'general/systemdriver/db_factory.php');
use \pateo\general\systemdriver\db_factory;
class_exists('\pateo\account\datadriver\dbconfig\config_account') || include ( 'account/datadriver/dbconfig/config_account.php' );
use \pateo\account\datadriver\dbconfig\config_account;
class DBConnect {
	
	/**
	 * crm数据连接[没有db配置信息方法]
	 * @return boolean|obj
	 * @example <pre>
	 * $objDBConnect = Factory::getObj( 'DBConnect', LIBS_PATH );
	 * $objCrmConnect = $objDBConnect -> getCrmConnect();
	 * </pre>
	 */
	public function getCrmConnect ( ) {
		global $roadbook_config;
		if( empty( $roadbook_config ) ){
			return false;
		}
		$_arrDBInfo = $roadbook_config[array_rand($roadbook_config)];
		$_objDB = db_factory::get_singleton_driver("mysql5", $_arrDBInfo);
		if( !$_objDB -> is_connected() )
		{
			return false;
		}
		
		return $_objDB;
	}
	
	/**
	 * account数据库连接[有db配置信息方法]
	 * @param str $_strDBType 主从配置  默认主库 $_strDBType = "slave" | $_strDBType = "master"
	 * @return boolean|unknown
	 */
	public function getAccountConnect ( $db_parm, $_strDBType = "master" ) {
		$_arrDBInfo = array();
		$_objAccount = new config_account( );
		switch ( $_strDBType ) {
			case 'slave':
				$_arrDBInfo = $_objAccount -> get_slave_config ( $db_parm );
				break;
			case 'master':
			default:
				$_arrDBInfo = $_objAccount -> get_master_config ( $db_parm );
				break;
			
		}
		$_objDB = db_factory::get_singleton_driver("mysql5", $_arrDBInfo);
		if( !$_objDB -> is_connected() )
		{
			return false;
		}
		return $_objDB;
	}

}